<?php
namespace App\Services\H5;

use Illuminate\Support\Facades\DB;

class IndexService extends BaseService
{
    /**
     * 首页
     */

    /**
     * 更新H5信息
     * @param string $h5code H5编号
     */
    public function toH5code($code, $h5code, $scene_id)
    {
        //判断有效吗码
        $qrOne = (array) $this->dbConn($this->dbQr)
            ->where('code', $code)
            ->first();
        if (empty($qrOne)) {
            $arys = [
                'code_used' => 0, //无
            ];
            return $this->toback(0, '', $arys);
        }
        //扫码日志递增
        $this->dbConn($this->dbQrlog)->insertGetId([
            'code' => $code,
            'h5code' => $h5code,
            'tmp_id' => $qrOne['tmp_id'],
            'errMsg' => '扫码成功',
            'client_type' => 1,
            'addtime' => toDate(),
        ]);
        //获取业务数据
        $tmpOne = (array) $this->dbConn($this->dbTmp)->where('code', $code)->first();
        if (empty($tmpOne)) {
            $totype = 2; $toid = $scene_id;
        } else {
            $totype = 1; $toid = $tmpOne['id'];
        }
        //更新H5编号
        $uOne = (array) $this->dbConn($this->dbH5xcx)
            ->where('h5code', $h5code)
            ->first();
        if (empty($uOne)) {
            $this->dbConn($this->dbH5xcx)->insertGetId([
                'code' => $code, 'h5code' => $h5code,
                'totype' => $totype, 'toid' => $toid,
                'addtime' => toDate(),
            ]);
        }
        //看看码是否为空
        $tmpAry = (array) $this->dbConn($this->dbTmp)
            ->where('del', 1)
            ->where('code', $code)
            ->first();
        $arys = [
            'code_used' => empty($tmpAry) ? 1 : 2, //空码，已使用
        ];
        return $this->toback(0, '', $arys);
    }

//    /**
//     * 新增
//     * @param string $phone 手机号
//     * @param string $h5code H5编号
//     */
//    public function setPhone($phone, $h5code)
//    {
//        try {
//            //更新用户
//            $username = 'U'.toCode();
//            $uid = $this->dbConn($this->dbUser)->insertGetId([
//                'phone' => $phone,
//                'username' => $username,
//                'addtime' => toDate(),
//            ]);
//            if (!$uid) {
//                return $this->toback(-1, 'error');
//            }
//            //更新H5编号
//            $res = $this->dbConn($this->dbH5code)->where('h5code', $h5code)
//                ->insertGetId(['uid' => $uid, 'utime' => toDate()]);
//            if (!$res) {
//                return $this->toback(-1, 'log error');
//            }
//            //更新扫码日志
//            $this->dbConn($this->dbQrlog)->where('uid', 0)
//                ->where('h5code', $h5code)
//                ->update(['uid' => $uid]);
//            return $this->toback();
//        } catch (\Exception $e) {
//            echo $e->getMessage();
//            return $this->toback(-1, 'error');
//        }
//    }

    /**
     * 场景列表
     */
    public function sceneIndex($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbScene)->where('del', 1);
        $total = $db->count();
        $res = $db->orderBy('id', 'asc')
            ->skip($start)->take($pages['limit'])
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $thumb = file_exists($v['thumb']) ? $v['thumb'] : '';
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'thumb' => $thumb,
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 场景内容列表
     */
    public function sceneCntIndex($sceneId)
    {
        $res = $this->dbConn($this->dbSceneCnt)
            ->where('del', 1)
            ->where('scene_id', $sceneId)
            ->orderBy('id', 'asc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'id' => $v['id'],
                'type' => $v['type'],
                'cnt' => $v['cnt'],
            ];
        }
        return $this->toback(0, '', $arys);
    }
}